package com.yyfax.pay.order.data.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.util.CommonIdUtils;
import com.yyfax.pay.order.data.dao.PayinBatchOrderDao;
import com.yyfax.pay.order.data.dao.PayinBatchOrderLogDao;
import com.yyfax.pay.order.data.service.PayinBatchOrderService;
import com.yyfax.pay.order.data.service.PayinOrderService;
import com.yyfax.pay.order.model.po.PayinBatchOrderLogPO;
import com.yyfax.pay.order.model.po.PayinBatchOrderPO;
import com.yyfax.pay.order.model.to.payin.PayinBatchSnapshotVo;
import com.yyfax.pay.order.model.to.payin.PayinOrderVO;

/**
 * 批量代扣订单服务实现
 *
 * @author maojian
 * @date 2017-04-26 11:09:15
 * @since 1.0.0
 */
@Service
public class PayinBatchOrderServiceImpl extends BaseServiceImpl implements PayinBatchOrderService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PayinBatchOrderDao payinBatchOrderDao;
	
	@Autowired
	private PayinBatchOrderLogDao payinBatchOrderLogDao;
	
	@Autowired
	private PayConstants payConstants;
	
	@Autowired
	private PayinOrderService payinOrderService;

	public PayinBatchOrderServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}


	@Override
	public String saveAndReturnBusiKey(PayinBatchOrderPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空，返回-1");
			return null;
		}

		String payoutBatchOrderId = CommonIdUtils.generateId(PayTypeEnum.BATCH_PAY_IN, po.getMerchantCode(), po.getOrderId());
		po.setPayinBatchOrderId(payoutBatchOrderId);

		if (StringUtils.isBlank(po.getExpireTime())) { // 如果没有传订单过期时间，则默认生成过期时间
			long expireTime = DateUtil.str2DateTime(po.getOrderTime()).getTime() + payConstants.getDefaultExpireTime();
			po.setExpireTime(DateUtil.milli2str(expireTime));
		}

		po.setCreateTime(DateUtil.getCurrentDateTime());
		payinBatchOrderDao.insertReturnPK(po);

		return payoutBatchOrderId;
	}


	@Override
	public PayinBatchOrderPO queryByMerchantCodeAndOrderId(String merchantCode, String orderId) throws Exception {
		if (StringUtils.isBlank(orderId) && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 orderId/merchantCode 为空 ");
			return null;
		}

		return payinBatchOrderDao.selectByOrderIdAndMerchantCode(orderId, merchantCode);
	}


	@Override
	public PayinBatchSnapshotVo querySnapshotByMerchantCodeAndOrderId(String merchantCode, String orderId) throws Exception {
		PayinBatchSnapshotVo payinBatchVo = new PayinBatchSnapshotVo();
		PayinBatchOrderPO payinBatchOrderPO = queryByMerchantCodeAndOrderId(merchantCode, orderId);
		
		BeanUtils.copyProperties(payinBatchOrderPO, payinBatchVo);

		List<PayinBatchOrderLogPO> payinBatchLog = payinBatchOrderLogDao.selectListByOrderId(merchantCode, orderId);
		payinBatchVo.setOrderLogPOs(payinBatchLog);
		
		List<PayinOrderVO> payinOrderVOs = payinOrderService.queryPayinOrderVoList(merchantCode, payinBatchOrderPO.getPayinBatchOrderId());
		payinBatchVo.setPayinOrderVOs(payinOrderVOs);
		
		// 判断订单是否失效
		if (DateUtil.str2DateTime(payinBatchOrderPO.getExpireTime()).getTime() <= System.currentTimeMillis()) {
			payinBatchVo.setExpire(true);
		}

		return payinBatchVo;
	}

	@Override
	public int modify(PayinBatchOrderPO payinBatchOrderPO) throws Exception {
		if(payinBatchOrderPO == null){
			return -1;
		}
		
		return payinBatchOrderDao.update(payinBatchOrderPO);
	}
	
	
}
